package com.jcxt.bus.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcxt.bus.domain.BusRenter;
import com.jcxt.bus.service.IBusRenterService;
import com.jcxt.bus.vo.RenterVo;
import com.jcxt.sys.common.DataGridView;
import com.jcxt.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author klj
 * @since 2020-12-26
 */
@RestController
@RequestMapping("/renter")
public class BusRenterController {

    @Autowired
    private IBusRenterService iBusRenterService;

    /**
     * 查询
     */
    @GetMapping("/loadAll")
    public DataGridView loadAllCustomer(RenterVo renterVo) {
        IPage<BusRenter> page = new Page<>(renterVo.getPage(), renterVo.getLimit());
        iBusRenterService.page(page, Wrappers.<BusRenter>lambdaQuery()
                .eq(BusRenter::getIsDeleted, 0)
                .like(StringUtils.isNotBlank(renterVo.getName()), BusRenter::getName, renterVo.getName())
                .eq(Objects.nonNull(renterVo.getSex()), BusRenter::getSex, renterVo.getSex())
                .like(StringUtils.isNotBlank(renterVo.getMobile()), BusRenter::getMobile, renterVo.getMobile())
        );
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 添加
     */
    @PostMapping("add")
    public ResultObj addCustomer(RenterVo renterVo) {
        try {
            iBusRenterService.save(renterVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public ResultObj updateCustomer(RenterVo renterVo) {
        try {
            iBusRenterService.updateById(renterVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public ResultObj deleteCustomer(Integer id) {
        if (Objects.isNull(id)) return ResultObj.DELETE_ERROR;
        try {
            iBusRenterService.update(Wrappers.<BusRenter>lambdaUpdate().set(BusRenter::getIsDeleted, 1).eq(BusRenter::getId, id));
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDelete")
    public ResultObj batchDeleteCustomer(RenterVo renterVo) {
        try {
            if (Objects.isNull(renterVo.getIds()) || renterVo.getIds().length == 0) return ResultObj.DELETE_ERROR;
            iBusRenterService.update(Wrappers.<BusRenter>lambdaUpdate()
                    .set(BusRenter::getIsDeleted, 1)
                    .in(BusRenter::getId, renterVo.getIds()));
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
